package oa.hleast.xswl.service.store.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.constant.AuthConstant;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.mapper.store.StoreInfoMapper;
import oa.hleast.xswl.mapper.store.StorePayInfoMapper;
import oa.hleast.xswl.pojo.domain.StorePayInfo;
import oa.hleast.xswl.pojo.form.store.StorePayInfoForm;
import oa.hleast.xswl.pojo.vo.store.StorePayInfoConfig;
import oa.hleast.xswl.pojo.vo.store.StorePayInfoVo;
import oa.hleast.xswl.service.store.StorePayInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: 302
 * @date: 2021-10-22 09:32:39
 * @description: 商家支付信息表(StorePayInfo)接口实现
 */
@Slf4j
@Transactional
@AllArgsConstructor
@Service("storePayInfoService")
public class StorePayInfoServiceImpl extends ServiceImpl<StorePayInfoMapper, StorePayInfo> implements StorePayInfoService {

    private final StoreInfoMapper storeInfoMapper;

    @Override
    public StorePayInfoVo getStorePayVoByStoreId(Long id) {

        List<StorePayInfo> storePayInfos = this.list(new LambdaQueryWrapper<StorePayInfo>()
                .eq(StorePayInfo::getStoreId, id)
                .last("limit 1")
        );

        if (CollectionUtils.isEmpty(storePayInfos)) {
            return null;
        }

        StorePayInfo storePayInfo = storePayInfos.get(0);
        StorePayInfoVo vo = new StorePayInfoVo();
        BeanUtil.copyProperties(storePayInfo, vo);

        return vo;
    }

    @Override
    public StorePayInfo getStorePayByStoreId(Long id) {

        List<StorePayInfo> storePayInfos = this.list(new LambdaQueryWrapper<StorePayInfo>()
                .eq(StorePayInfo::getStoreId, id)
                .last("limit 1")
        );

        if (CollectionUtils.isEmpty(storePayInfos)) {
            return null;
        }

        return storePayInfos.get(0);
    }

    /**
     * 更新商店的支付信息
     */
    @Override
    public boolean updateStorePayInfo(StorePayInfoForm form) {

        StorePayInfo storePayInfo = this.getById(form.getId());
        if (ObjectUtils.isEmpty(storePayInfo)) {
            throw new BizException("此数据不存在");
        }

        Integer hasAlipayAuth = form.getHasAlipayAuth();
        String alipayAppId = form.getAlipayAppId();
        String alipayAppCertPublicKey = form.getAlipayAppCertPublicKey();

        Integer hasWxAuth = form.getHasWxAuth();
        String wxSubMchId = form.getWxSubMchId();

        // 检查参数
        if (1 == hasAlipayAuth) {
            if (ObjectUtils.isEmpty(alipayAppId)) {
                throw new BizException("缺少支付宝应用ID");
            }
            if (ObjectUtils.isEmpty(alipayAppCertPublicKey)) {
                throw new BizException("缺少支付宝公钥证书");
            }
        }

        if (1 == hasWxAuth) {
            if (ObjectUtils.isEmpty(wxSubMchId)) {
                throw new BizException("缺少微信子商户号");
            }
        }

        BeanUtil.copyProperties(form, storePayInfo);
        log.info("storePayInfo = {}", storePayInfo);
        boolean update = this.updateById(storePayInfo);
        log.info("update = {} ,storePayInfo = {}", update, storePayInfo);

        return update;
    }

    @Override
    public StorePayInfoConfig getStorePayConfig(Long storeId) {
        // 获取商家的支付配置
        StorePayInfo pojo = this.list(new LambdaQueryWrapper<StorePayInfo>()
                .eq(StorePayInfo::getStoreId, storeId)
                .last("limit 1")).get(0);

        StorePayInfoConfig storePayInfoVo = new StorePayInfoConfig();
        BeanUtil.copyProperties(pojo, storePayInfoVo);


        // 客户端判断
        String clientId = RequestUtils.getClientId();

        log.info("clientId = {}", clientId);
        switch (clientId) {
            case AuthConstant.MP_CLIENT_ID:
                // 微信小程序端 只支持微信
                storePayInfoVo.setHasAlipayAuth(1);
                break;
            case AuthConstant.MP_CLIENT_CITY_BRAIN_APP:
                //城市大脑认证 只支持微信
                storePayInfoVo.setHasAlipayAuth(0);
                break;
            case AuthConstant.MP_ZJFWW_APP:
                //浙里办 -文旅红 只支持支付宝
                storePayInfoVo.setHasWxAuth(0);
                break;
            case AuthConstant.MP_ZJFWW_WLYY_APP:
                //浙里办 -文旅预约 只支持支付宝
                storePayInfoVo.setHasWxAuth(0);
                break;
            default:
                break;
        }

        return storePayInfoVo;
    }


}
